package space.panker916.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import space.panker916.annotation.LoginRequired;
import space.panker916.event.EventProducer;
import space.panker916.pojo.Event;
import space.panker916.pojo.Page;
import space.panker916.pojo.R;
import space.panker916.pojo.User;
import space.panker916.service.FollowService;
import space.panker916.service.UserService;
import space.panker916.utils.HostHolder;

import java.util.List;
import java.util.Map;

import static space.panker916.utils.Constants.ENTITY_TYPE_USER;
import static space.panker916.utils.Constants.TOPIC_FOLLOW;

@Controller
public class FollowController {
    @Autowired
    private FollowService followService;

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @PostMapping("/follow")
    @ResponseBody
    @LoginRequired
    public R follow(int entityType, int entityId) {
        User user = hostHolder.getUser();
        followService.follow(user.getId(), entityType, entityId);

        // 触发关注事件
        Event event = new Event();
        event.setTopic(TOPIC_FOLLOW);
        event.setUserId(hostHolder.getUser().getId());
        event.setEntityType(entityType);
        event.setEntityId(entityId);
        event.setEntityUserId(entityId);

        eventProducer.fireEvent(event);

        return new R(0, "已关注！");
    }


    @PostMapping("/unFollow")
    @ResponseBody
    @LoginRequired
    public R unFollow(int entityType, int entityId) {
        User user = hostHolder.getUser();
        followService.unFollow(user.getId(), entityType, entityId);
        return new R(0, "已取消关注！");
    }

    @GetMapping("/followees/{userId}")
    public String getFollowees(@PathVariable("userId") int userId, Model model, Page page) {
        User user = userService.findUserById(userId);
        if (user == null) throw new RuntimeException("该用户不存在！");
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));
        model.addAttribute("page", page);

        List<Map<String, Object>> userList = followService.findFollowees(userId, page.getOffset(), page.getLimit());
        for (Map<String, Object> map : userList) {
            User tempUser = (User) map.get("user");
            map.put("hasFollowed", hasFollowed(tempUser.getId()));      // 这是干啥用的QAQ 。。。
        }
        model.addAttribute("users", userList);

        return "/site/followee";
    }

    @GetMapping("/followers/{userId}")
    public String getFollowers(@PathVariable("userId") int userId, Model model, Page page) {
        User user = userService.findUserById(userId);
        if (user == null) throw new RuntimeException("该用户不存在！");
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followers/" + userId);
        page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));
        model.addAttribute("page", page);

        List<Map<String, Object>> userList = followService.findFollowers(userId, page.getOffset(), page.getLimit());
        for (Map<String, Object> map : userList) {
            User tempUser = (User) map.get("user");
            map.put("hasFollowed", hasFollowed(tempUser.getId()));      // 这是干啥用的QAQ 。。。
        }
        model.addAttribute("users", userList);

        return "/site/follower";
    }

    private boolean hasFollowed(int userId) {
        if (hostHolder.getUser() == null) return false;
        return followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
    }
}
